﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class modify : System.Web.UI.Page
{
    protected BLL.Users blluser = new BLL.Users();
    protected void Page_Load(object sender, EventArgs e)
    {
        #region 从数据库读取数据显示出来
        //从数据库读取数据显示出来
        //注意是第一次访问的时候才执行
        if (!IsPostBack)
        {
            if (Session["currentUser"] != null)
            {
                if (Request.QueryString["id"] != null)
                {
                    int id = Convert.ToInt32(Request.QueryString["id"]);
                    #region MyRegion
                    try
                    {
                        MODEL.Users model = new MODEL.Users();
                        model = blluser.getModel(id);
                        //地址栏不出错
                        if (model != null)
                        {
                            txtName.Text = model.UName;
                            //txtPwd.Text = model.Pwd;
                            //txtPwd2.Text = model.Pwd;
                            txtPwd.Attributes.Add("value", model.Pwd.Substring(0,6));
                            txtPwd2.Attributes.Add("value", model.Pwd.Substring(0,6));
                            string[] question = { "您小学的名字?", "您最喜欢的地方？", "您最喜欢的演员？" };
                            ddlQuestion.DataSource = question;
                            ddlQuestion.DataBind();
                            ddlQuestion.SelectedItem.Text = model.Question.ToString();
                            txtAnswer.Value = model.Answer.ToString();
                            txtEmail.Text = model.Email;
                            txtQQ.Text = model.QQ;
                            txtPhone.Text = model.Phone;
                        }
                        else
                        {
                            Response.Redirect("index.aspx");
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    } 
                    #endregion
                }
            }
            else
            {
                Response.Redirect("index.aspx");
            }
        } 
        #endregion
    }

    #region 修改资料
    /// <summary>
    /// 修改资料
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ibtnReg_Click(object sender, ImageClickEventArgs e)
    {
        //提示用户是否修改
        if (Page.IsValid && checkVcode())
        {
            //MessageBox.Redirect(this, "return confirm('确定要更改吗?');window.location='del.ashx?id="+model.Id.ToString()+"';}");
            MODEL.Users model2 = new MODEL.Users();
            string pwd;
            if (Session["currentUser"] != null)
            {
                //通过session来取当前用户的Id
                model2.UId = ((MODEL.Users)Session["currentUser"]).UId;
                pwd = ((MODEL.Users)Session["currentUser"]).Pwd.Substring(0,6);
                //与数据库中的密码一样则没修改过，否则认为修改过，则MD5加密
                if (pwd == txtPwd.Text.Trim())
                {
                    model2.Pwd = ((MODEL.Users)Session["currentUser"]).Pwd;
                }
                else
                {
                    //如果是管理员 则密码不用MD5加密
                    if (((MODEL.Users)Session["currentUser"]).UserRoleId == 2 || ((MODEL.Users)Session["currentUser"]).UserRoleId == 3)
                    {
                        model2.Pwd = txtPwd.Text.Trim();
                    }
                    //否则MD5加密
                    else
                    {
                        model2.Pwd = common.GetMD5(txtPwd.Text.Trim());
                    }
                }
            }
            model2.UName = txtName.Text.Trim();
            
            model2.Question = ddlQuestion.SelectedItem.ToString().Trim();
            model2.Answer = txtAnswer.Value.ToString().Trim();
            model2.Email = txtEmail.Text.Trim();
            model2.QQ = txtQQ.Text.Trim();
            model2.Phone = txtPhone.Text.Trim();
            try
            {
                if (blluser.modify(model2))
                {
                    MODEL.Users model = new MODEL.Users();
                    if (Session["currentUser"] != null)
                    {
                        model = blluser.getModel(((MODEL.Users)Session["currentUser"]).UId);
                        //注意：修改之后得重新把model实体写进session里面
                        Session["currentUser"] = model;
                    }
                    MessageBox.Show(Page, "修改成功");
                    MessageBox.script(Page, "window.location='index.aspx';");
                }
                else
                {
                    MessageBox.Show(Page, "用户已存在或系统原因导致修改失败");
                    MessageBox.script(Page, "window.location='index.aspx';");
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        } 
    }
    #endregion

    #region  检查验证码是否正确
    /// <summary>
    /// 检查验证码是否正确
    /// </summary>
    /// <returns></returns>
    private bool checkVcode()
    {
        if (Session["vCode"] != null)
        {
            if (txtVCode.Text.Trim() == Session["vCode"].ToString())
            {
                return true;
            }
            else
            {
                MessageBox.Show(this, "验证码错误");
                return false;
            }
        }
        else
        {
            MessageBox.Show(this, "验证码过期了");
            return false;
        }
    }
}
    #endregion
